package com.kin207.netty.context;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.kin207.netty.server.IServer;
import com.kin207.netty.session.Session;
import com.kin207.netty.session.SessionManagerAbs;

import io.netty.channel.ChannelHandlerContext;

public class RequestContext {
	final static Logger logger = LoggerFactory.getLogger(RequestContext.class);
	
	public final static ThreadLocal<RequestContext> curr = new ThreadLocal<RequestContext>();
	
	private ChannelHandlerContext channelHandlerContext;
	private String message;
	private Session session;
	private IServer server;
	private Object params;
	
	public RequestContext(ChannelHandlerContext channelHandlerContext) {
		super();
		this.channelHandlerContext = channelHandlerContext;
	}
	public RequestContext(IServer server, ChannelHandlerContext channelHandlerContext) {
		super();
		this.server = server;
		this.channelHandlerContext = channelHandlerContext;
	}
	public RequestContext(ChannelHandlerContext channelHandlerContext, String message) {
		super();
		this.channelHandlerContext = channelHandlerContext;
		this.message = message;
	}
	public RequestContext(IServer server, ChannelHandlerContext channelHandlerContext, String message) {
		super();
		this.server = server;
		this.channelHandlerContext = channelHandlerContext;
		this.message = message;
	}
	public RequestContext(ChannelHandlerContext channelHandlerContext, String message,
			Session session) {
		super();
		this.channelHandlerContext = channelHandlerContext;
		this.message = message;
		this.session = session;
	}
	public RequestContext(IServer server, ChannelHandlerContext channelHandlerContext, String message,
			Session session) {
		super();
		this.server = server;
		this.channelHandlerContext = channelHandlerContext;
		this.message = message;
		this.session = session;
	}
	
	public SessionManagerAbs getSessionManager() {
		return this.server.getSessionManager();
	}
	public ChannelHandlerContext getChannelHandlerContext() {
		return channelHandlerContext;
	}
	public String getMessage() {
		return message;
	}
	public Session getSession() {
		if(session==null){
			session = this.server.getSessionManager().getSession(this.channelHandlerContext);
		}
		return session;
	}
	
	public void wirte(String msg){
		this.server.getSessionManager().sendMessage(channelHandlerContext, msg,System.currentTimeMillis());
	}
	public IServer getServer() {
		return server;
	}
	public Object getParams() {
		return params;
	}
	public void setParams(Object params) {
		this.params = params;
	}
	
	
	
}
